<template>
  <div v-if="visible" class="city-selector-modal" @click.self="close">
    <div class="selector-container">
      <div class="selector-header">
        <div class="header-item">
          <span class="header-label">按省份选择</span>
          <select v-model="selectedProvince" class="header-select">
            <option value="">省份</option>
             <!-- 动态省份选项 -->
            <option value="北京">北京</option>
            <option value="上海">上海</option>
          </select>
        </div>
        <div class="header-item">
          <span class="header-label">直接搜索</span>
          <div class="search-wrapper">
            <input type="text" v-model="search" placeholder="城市名称" class="header-input"/>
            <i class="search-icon"></i>
          </div>
        </div>
        <button class="close-btn" @click="close">×</button>
      </div>
      <div class="selector-content">
        <div class="hot-cities-section" v-show="tab === 'province'">
          <div class="section-title">热门城市</div>
          <div class="city-tags">
            <span v-for="city in hotCities" :key="city" class="city-tag" @click="choose(city)">{{ city }}</span>
          </div>
        </div>
        <div class="alphabet-nav" v-show="tab === 'province'">
          <span v-for="letter in alphabet" :key="letter" @click="scrollToLetter(letter)">{{ letter }}</span>
        </div>
        <div class="city-list-container" ref="cityListContainer">
          <div v-if="tab === 'province'">
            <div v-for="(cities, letter) in groupedCities" :key="letter" :ref="el => cityGroups[letter] = el">
              <div class="letter-group">
                <div class="letter">{{ letter }}</div>
                <div class="cities">
                  <span v-for="city in cities" :key="city" class="city-tag" @click="choose(city)">{{ city }}</span>
                </div>
              </div>
            </div>
          </div>
          <div v-else class="search-result">
            <div class="cities">
                <span v-for="city in filteredCities" :key="city" class="city-tag" @click="choose(city)">{{ city }}</span>
            </div>
            <div v-if="search && !filteredCities.length" class="no-result">
              没有找到匹配的城市
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, watch } from 'vue';
import cityData from '../assets/cityData.json';

const props = defineProps({
  visible: Boolean
});

const emit = defineEmits(['update:visible', 'select']);

const tab = ref('province');
const search = ref('');
const selectedProvince = ref('');
const hotCities = ['全国', '北京', '上海', '广州', '深圳', '杭州', '天津', '西安', '苏州', '武汉', '厦门', '长沙', '成都', '郑州', '重庆'];
const allCities = cityData;

const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('');
const cityListContainer = ref(null);
const cityGroups = ref({});

const groupedCities = computed(() => {
  const groups = {};
  alphabet.forEach(letter => {
    const citiesInGroup = allCities.filter(city => city.letter.toUpperCase() === letter).map(c => c.name);
    if(citiesInGroup.length > 0) {
        groups[letter] = Array.from(new Set(citiesInGroup)); // 去重
    }
  });
  return groups;
});

const filteredCities = computed(() => {
  if (!search.value) return [];
  const searchTerm = search.value.toLowerCase();
  return allCities
    .filter(city => city.name.includes(search.value) || city.pinyin.includes(searchTerm))
    .map(city => city.name);
});

watch(search, (newValue) => {
    if(newValue) {
        tab.value = 'search';
    } else {
        tab.value = 'province'
    }
});

const selectTab = (selectedTab) => {
  tab.value = selectedTab;
};

const choose = (city) => {
  emit('select', city);
  close();
};

const close = () => {
  emit('update:visible', false);
};

const scrollToLetter = (letter) => {
  const el = cityGroups.value[letter];
  if (el) {
    el.scrollIntoView({ behavior: 'smooth' });
  }
};
</script>

<style scoped>
.city-selector-modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 1000;
  display: flex;
  justify-content: center;
  padding-top: 15vh;
}

.selector-container {
  width: 860px;
  height: fit-content;
  max-height: 70vh;
  display: flex;
  flex-direction: column;
}

.selector-header {
  display: flex;
  align-items: center;
  gap: 20px;
  background: #19c3ae;
  padding: 15px 30px;
  border-top-left-radius: 12px;
  border-top-right-radius: 12px;
  position: relative;
}
.header-item {
    display: flex;
    align-items: center;
    gap: 10px;
}
.header-label {
    color: white;
    font-size: 14px;
}
.header-select, .header-input {
    background-color: white;
    border: 1px solid #dcdfe6;
    border-radius: 4px;
    padding: 0 15px;
    height: 38px;
    line-height: 38px;
    font-size: 14px;
}
.header-select {
    min-width: 150px;
}
.header-input {
    width: 200px;
}
.search-wrapper {
    position: relative;
}
.search-icon {
    position: absolute;
    right: 12px;
    top: 50%;
    transform: translateY(-50%);
    width: 16px;
    height: 16px;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' fill='%23C0C4CC' class='bi bi-search' viewBox='0 0 16 16'%3E%3Cpath d='M11.742 10.344a6.5 6.5 0 1 0-1.397 1.398h-.001c.03.04.062.078.098.115l3.85 3.85a1 1 0 0 0 1.415-1.414l-3.85-3.85a1.007 1.007 0 0 0-.115-.1zM12 6.5a5.5 5.5 0 1 1-11 0 5.5 5.5 0 0 1 11 0z'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
}

.close-btn {
  position: absolute;
  top: -10px;
  right: -10px;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: #fff;
  border: 1px solid #eee;
  box-shadow: 0px 2px 5px rgba(0,0,0,0.1);
  font-size: 20px;
  line-height: 20px;
  text-align: center;
  color: #999;
  cursor: pointer;
  padding: 0;
}
.selector-content {
  background: #fff;
  padding: 0 30px 20px;
  border-bottom-left-radius: 12px;
  border-bottom-right-radius: 12px;
  overflow-y: auto;
}

/* Custom Scrollbar */
.selector-content::-webkit-scrollbar {
    width: 6px;
}
.selector-content::-webkit-scrollbar-thumb {
    background: #ccc;
    border-radius: 3px;
}
.selector-content::-webkit-scrollbar-track {
    background: #f1f1f1;
}

.section-title {
  color: #999;
  font-size: 14px;
  margin: 20px 0 10px;
}

.city-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
}

.city-tag {
  background-color: #f5f5f5;
  color: #333;
  padding: 6px 16px;
  border-radius: 4px;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.2s ease;
}

.city-tag:hover {
  background-color: #19c3ae;
  color: #fff;
}

.alphabet-nav {
  margin: 20px 0;
  display: flex;
  gap: 12px;
  font-size: 14px;
  color: #888;
}
.alphabet-nav span {
    cursor: pointer;
}
.alphabet-nav span:hover {
    color: #19c3ae;
}

.letter-group {
    display: flex;
    margin: 15px 0;
    align-items: flex-start;
}
.letter {
    width: 40px;
    font-size: 16px;
    color: #19c3ae;
    font-weight: bold;
    padding-top: 6px;
}
.cities {
    flex: 1;
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
}
.no-result {
    text-align: center;
    color: #999;
    padding: 40px;
}
</style> 